<?php
namespace app\admin\controller;
use think\Db;
class Department extends Base
{
  //列表
  public function lists()
  {
      if(is_post()){
          $cur_page     = input('page');
          $keyword  = input('keyword');
          $min_time = input('min_time');
          $max_time = input('max_time');
          $sql = "
          select
              [1]
          from
              department
          ";
          $param = [];
          $w = ['where'];
          if(!empty($min_time)){
              $w[] = ' and create_time>?';
              $param[] = strtotime($min_time);
          }
          if(!empty($max_time)){
              $w[] = ' and create_time<?';
              $param[] = strtotime($max_time);
          }
          if(!empty($keyword)){
              $w[] = ' and title like ?';
              $param[] = "%{$keyword}%";
          }
          #条件判断
          if (count($w)>1){
            $w[1] = str_replace("and","",$w[1]);
            $sql .= implode(" ",$w);
          }
          #分页
          $page_size = c('a.page_size');
          $page_html = com_list_page($sql, $param, $cur_page,$page_size);

          #内容
          $start = ($cur_page - 1) * $page_size;
          $field = "*";
          $sql_list = str_replace("[1]",$field,$sql);
          $sql_list .= " order by id desc limit {$start},{$page_size}";
          $rslist = Db::query($sql_list, $param);
          foreach ($rslist as $k => $v) {
              $rslist[$k]['create_time'] = com_formate_time($v['create_time']);
              $rslist[$k]['checkbox'] = com_checkbox($v['id']);
              $rslist[$k]['action']  = com_btn_edit("/department/edit/id/{$v['id']}","800px","400px");
              $rslist[$k]['action']  .= com_btn_del("/department/delete/id/{$v['id']}", "您确定要删除【{$v['title']}】？");
          }
          $body_html = com_list_body($rslist, ['checkbox','title','create_time','action']);
          com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
      }
      return view();
  }
  #添加
  function add(){
    if(is_post()){
          $d = input();
          $rule = [
              'title'=>  'require',
          ];
          $msg = [
            'title.require'   =>  '请输入标题',
          ];
          com_validate($rule, $msg, $d);
          $d['create_time'] = time();
          $d['update_time'] = time();
          Db::name("department")->insert($d);
          com_check(['suc_msg'=>'添加成功!','suc_js'=>'alert|close|reload']);
    }
    return view();
  }
  #编辑
  function edit($id){
    if(is_post()){
          $d = input();
          $rule = [
              'id'=>  'require',
              'title'=>  'require',
          ];
          $msg = [
            'id.require'   =>  '请输入id',
            'title.require'   =>  '请输入标题',
          ];
          com_validate($rule, $msg, $d);
          if(!isset($d['power_ids']))
              $d['power_ids'] = [];
          #没有权限的id,存入department表的power_ids中
          $power = Db::name('power')->column('id');
          $no_power_ids = array_diff($power,$d['power_ids']);
          $s = ",".implode(",",$no_power_ids).",";
          Db::name("department")->update(['id'=>$d['id'],'title'=>$d['title'],'power_ids'=>$s,'update_time'=>time()]);
          com_check(['suc_msg'=>'修改成功!','suc_js'=>'alert|close|reload']);
    }
    $info = Db::name('department')->find($id);
    $power = Db::query('select * from power');
    $power_ids = [];
    if($info['power_ids']){
        $power_ids = array_filter(explode(",",$info['power_ids']));
    }
    return view('',['info'=>$info,'power'=>$power,'power_ids'=>$power_ids]);
  }
  #删除
  function delete($id){
    $res = Db::query("select id from admin_user where department_ids like ?",["%,{$id},%"]);
    if($res){
        com_error("该部门有用户,不能删除!");
    }else{
        Db::name("department")->delete($id);
    }
    com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
  }
  #批量删除
  function delall(){
    if(is_post()){
        $ids = input('ids/a');
        foreach($ids as $id){
            $res = Db::query("select id from admin_user where department_ids like ?",["%,{$id},%"]);
            if($res){
                com_error("该部门有用户,不能删除!");
                com_check();
            }else{
                Db::name("department")->delete($id);
            }
        }
        com_check(['suc_msg'=>'删除成功!','suc_js'=>'alert|close|reload']);
    }
  }

}
